﻿// app.title = '2015 年上证指数';
var myChart;// = echarts.init(document.getElementById('myChart'));
// window.onresize = myChart.resize;

// 数据意义：开盘(open)，收盘(close)，最低(lowest)，最高(highest)
var data = {categoryData:[], values:[]};
function InitializaChart(jsonDate, range, txt_len, limit, isRest) {
    if (isRest == 2) {
        limit = 1;
    }

    var new_game_data = [];
    for (var i=0; i<(limit*30); i++) {
        new_game_data.push(jsonDate[i])
    }
    if (range != "M1" && range != "1M" ) {
        new_game_data = format_data(new_game_data, limit);
    }
    if (isRest == 1) {
        if (range != "M1") {
            k_line(new_game_data, txt_len);
        } else {
            line(new_game_data, txt_len);
        }
    } else {
        initialize_line(new_game_data, txt_len);
    }
}

function splitData(rawData) {
    var categoryData = [];
    var values = [];
    for (var i = 0; i < rawData.length; i++) {
        categoryData.push(rawData[i].splice(0, 1)[0]);
        values.push(rawData[i]);
    }
    return {
        categoryData: categoryData,
        values: values
    };
}
function format_data(d, limit) {
    var arr = group(d, limit);
    var new_arr = Array();
    for (var i=0; i<arr.length; i++) {
        var max_arr = Array();
        var min_arr = Array();
        for (var j=0; j<arr[i].length; j++) {
            max_arr.push(arr[i][j].max_value);
            min_arr.push(arr[i][j].min_value);
        }
        var time = arr[i][0].time;
        var open_value = arr[i][0].open_value;
        var close_value = arr[i][0].close_value;
        var max_value = Math.max.apply(null, max_arr);
        var min_value = Math.min.apply(null, min_arr);
        var obj = new Object();
        obj.time = time;
        obj.open_value = open_value;
        obj.close_value = close_value;
        obj.max_value = max_value;
        obj.min_value = min_value;
        new_arr.push(obj);
    }
    return new_arr;
}
function group(array, subGroupLength) {
    var index = 0;
    var newArray = [];
    while(index < array.length) {
        newArray.push(array.slice(index, index += subGroupLength));
    }
    return newArray;
}

function k_line(jsonDate, txt_len) {
    var color = "red";
    if (vue.type_prices[vue.pdata[vue.pi].id][1] == -1) {
        color = "#01b95a";
    }

    var len = 0;
    var new_data = new Array();
    var max_arr = new Array();
    var min_arr = new Array();
    for (var i = (jsonDate.length-1); i >= 0; i--) {
        var temp_data = [jsonDate[i].time, parseFloat(jsonDate[i].open_value), parseFloat(jsonDate[i].close_value), parseFloat(jsonDate[i].min_value), parseFloat(jsonDate[i].max_value)];
        new_data[len] = temp_data;
        max_arr.push(jsonDate[i].max_value);
        min_arr.push(jsonDate[i].min_value);
        len++;
    }
    data = splitData(new_data);
    var value = parseFloat(jsonDate[0].close_value).toFixed(txt_len);
    // var value = jsonDate[0].close_value;

    var max = Math.max.apply(null, max_arr);
    var min = Math.min.apply(null, min_arr);
    var interval = (max-min)/4.3;
    if(undefined !== myChart) {
        myChart.setOption({
            // backgroundColor: '#21202D',
            // legend: {show:false,left:'0%',top:'0%',animation:true,data:[$("#optionname").html(),'MA3','MA5'],inactiveColor: '#777',textStyle:{color:'#fff'}, selected:{'MA3':true,'MA5':true}},
            tooltip:{trigger: 'axis', formatter: function (params) {
                if (undefined !== params[0]) {
                    var res = params[0].name;
                    res += '<br/>开盘 : ' + params[0].value[0].toFixed(txt_len);
                    res += ' 最高 : ' + params[0].value[3].toFixed(txt_len);
                    res += '<br/>收盘 : ' + params[0].value[1].toFixed(txt_len);
                    res += ' 最低 : ' + params[0].value[2].toFixed(txt_len);
                    return res;
                }
            }},
            grid:{left: '2%',right:'2%',bottom:'0%',top:'5%',containLabel:true,animation:true},
            xAxis:{type:'category',data:data.categoryData,animation:true,boundaryGap: true,axisLine:{lineStyle:{color:'#eeeeee'}},axisLabel:{interval:3}},
            yAxis:{type:'value',interval:interval,max:max+interval/3,min:min-interval/3,position:'left',scale:true,animation:true,axisLine:{lineStyle:{color:'#eeeeee'}},splitLine:{show:true},axisLabel:{formatter:function(value){return parseFloat(value).toFixed(txt_len);}}},
            series:[
                {
                    name: $("#optionname").html(),connectNulls:true,animation:true,type:'candlestick',data:data.values,itemStyle:{normal:{color:'#fd1050',color0:'#0cf49b',lineStyle:{width: 1,color: '#fd1050',color0:'#0cf49b'}}},
                    markLine: {
                        animation:false,
                        symbol: ['none', 'none'],
                        lineStyle:{
                            normal:{
                                width:1
                            }
                        },
                        data : [[
                            {xAxis:0,yAxis:value,itemStyle:{normal:{color:color,label:{show:true,position:'start',textStyle:{fontSize:'13',fontWeight:'bold'}}}}},
                            {xAxis:(jsonDate.length-1),yAxis:value,value:value}
                        ]]
                    }
                }
            ]
        });

    }
}

function line(temp_data, txt_len) {
    var color = "red";
    if (vue.type_prices[vue.pdata[vue.pi].id][1] == -1) {
        color = "#01b95a";
    }

    var datas = new Array();
    var datetimes = new Array();

    for (var i = (temp_data.length-1); i >= 0; i--) {
        datas.push(temp_data[i].close_value);
        datetimes.push(temp_data[i].time);
    }

    var close_value = parseFloat(temp_data[0].close_value).toFixed(txt_len);

    var max = Math.max.apply(null, datas);
    var min = Math.min.apply(null, datas);
    var interval = (max-min)/4.3;
    if(undefined !== myChart) {
        myChart.setOption({
            // backgroundColor: '#21202D',
            // legend: {show:false,left:'0%',top:'0%',animation:true,data:[$("#optionname").html(),'MA3','MA5'],inactiveColor: '#777',textStyle:{color:'#fff'}, selected:{'MA3':true,'MA5':true}},
            tooltip:{trigger: 'axis', formatter: function (params) {
                if (undefined !== params[0]) {
                    var res = params[0].name;
                    res += "<br/>"+parseFloat(params[0].value).toFixed(txt_len);
                    return res;
                }
            }},
            grid:{left: '0%',right:'0%',bottom:'0%',top:'5%',containLabel:true,animation:true},
            xAxis:{type:'category',data:datetimes,animation:true,boundaryGap: false,axisLine:{lineStyle:{color:'#eeeeee'}},axisLabel:{interval:3}},
            yAxis:{type:'value',interval:interval,offset:0,position:'left',max:max+interval/3,min:min-interval/3,scale:true,animation:true,axisLine:{lineStyle:{color:'#eeeeee'}},splitLine:{show:true},axisLabel:{formatter:function(value){return parseFloat(value).toFixed(txt_len);}}},
            series:[
                {
                    name:'最新数据',connectNulls:true,animation:true,type:'line',data:datas,areaStyle:{normal: {}},
                    markLine: {
                        animation:false,
                        symbol: ['none', 'none'],
                        lineStyle:{
                            normal:{
                                width:1
                            }
                        },
                        data : [[
                            {xAxis:0,yAxis:close_value,itemStyle:{normal:{color:color,label:{show:true,position:'start',textStyle:{fontSize:'12',fontWeight:'bold'}}}}},
                            {xAxis:(temp_data.length-1),yAxis:close_value,value:close_value}
                        ]]
                    }
                }
            ],
            itemStyle:{
                normal:{
                    width:1,
                    color:"#6b6b6b"
                }
            }
        });
    }
}
function initialize_line(temp_data, txt_len) {
    var datas = new Array();
    var datetimes = new Array();
    for (var i = (temp_data.length-1); i >= 0; i--) {
        datas.push(temp_data[temp_data.length-1].close_value);
        datetimes.push(temp_data[i].time);
    }
    if(undefined !== myChart) {
        myChart.setOption({
            // backgroundColor: '#21202D',
            // legend: {show:false,left:'0%',top:'0%',animation:true,data:[$("#optionname").html(),'MA3','MA5'],inactiveColor: '#777',textStyle:{color:'#fff'}, selected:{'MA3':true,'MA5':true}},
            tooltip:{trigger: 'axis', formatter: function (params) {
                if (undefined !== params[0]) {
                    var res = params[0].name;
                    res += "<br/>"+parseFloat(params[0].value).toFixed(txt_len);
                    return res;
                }
            }},
            grid:{left: '0%',right:'0%',bottom:'0%',top:'5%',containLabel:true,animation:true},
            xAxis:{type:'category',data:datetimes,animation:true,boundaryGap: false,axisLine:{lineStyle:{color:'#eeeeee'}},axisLabel:{interval:3}},
            yAxis:{type:'value',position:'left',scale:true,animation:true,axisLine:{lineStyle:{color:'#eeeeee'}},splitLine:{show:true},axisLabel:{formatter:function(value){return parseFloat(value).toFixed(txt_len);}}},
            series:[
                {
                    name:'最新数据',connectNulls:true,animation:true,type:'line',data:datas
                }
            ],
            itemStyle:{
                normal:{
                    width:2,
                    color:"#6b6b6b"
                }
            }
        });
    }
}